namespace Digitalmes.Application.Mes;

/// <summary>
/// 生产工单明细表服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "mes")]
public sealed class ProdWoDetailService : IDynamicWebApiService
{
    private readonly SugarRepository<ProdWoDetail> _thisRepository;

    public ProdWoDetailService(SugarRepository<ProdWoDetail> thisRepository)
    {
        _thisRepository = thisRepository;
    }

    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<ProdWoDetailDto>> GetPagesAsync(PageParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .ToPageAsync(param.Page, param.Limit);
        return query.Adapt<PageResult<ProdWoDetailDto>>();
    }

    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<ProdWoDetailDto>> GetPagesByWoIdAsync(WoDetailPageParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .Where(s => s.WoId == param.WoId)
            .ToPageAsync(param.Page, param.Limit);
        return query.Adapt<PageResult<ProdWoDetailDto>>();
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<ProdWoDetailDto> GetAsync(long id)
    {
        var model = await _thisRepository.GetByIdAsync(id);
        return model.Adapt<ProdWoDetailDto>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> AddAsync(ProdWoDetailDto model)
    {
        return await _thisRepository.InsertAsync(model.Adapt<ProdWoDetail>());
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAsync(ProdWoDetailDto model)
    {
        return await _thisRepository.UpdateAsync(model.Adapt<ProdWoDetail>());
    }

    /// <summary>
    /// 删除,支持批量
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync([FromBody] List<long> ids) =>
        await _thisRepository.DeleteAsync(m => ids.Contains(m.Id));
}
